package davidlauzon.activerecord.recordset;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.List;

import davidlauzon.activerecord.nodes.field.Field;

public class JDBCRecordSet extends RecordSet
{
    private Statement _statement;
    private ResultSet _set;
        
    
    public JDBCRecordSet(Statement statement, ResultSet resultSet) {
        _statement = statement;
        _set       = resultSet;
    }  

  //  @Override
    public boolean next() throws SQLException {
        boolean hasNext = _set.next();
        
        if (!hasNext)       // Automatically close the result set if there is no more results
            close();
        
        return hasNext;
    }
    
    public boolean hasNext() throws SQLException
    {
    	return false;
    }
    
    // @Override
    public void close() {
        if (_set != null)
            try { _set.close(); } catch (SQLException e) {}
        if (_statement != null)
            try { _statement.close(); } catch (SQLException e) {}
    }
    
    private void moveCursorTo(int row) throws SQLException {
        if (row < 1)                // If there was no current row  
            _set.beforeFirst();     // We want next() to go to first row
        else                        // If there WAS a current row
            _set.absolute(row);     // Restore it
    }
    
    /** 
     * Determines the number of rows in a <code>ResultSet</code>. Upon exit, if the cursor was not 
     * currently on a row, it is just before the first row in the result set (a call to 
     * {@link ResultSet#next()} will go to the first row).
     * 
     * WARNING : "This is not a "free" operation. Most (all?) JDBC drivers use lazy load most (all?)
     *  of the time, meaning they fetch a buffer full of rows from the db to your Java memory at a 
     *  time. last() will force it to fetch them all. Right?"
     *  
     * @param set The <code>ResultSet</code> to check (must be scrollable). 
     * @return The number of rows. 
     * @throws SQLException If the <code>ResultSet</code> is not scrollable. 
     * @see #hasSingleRow(ResultSet) 
     */  
    private int countRows(ResultSet set) throws SQLException  
    {
        System.out.println("WARNING: JDBCRecordSet::countRows() may require to fetch all records.");
        
        int currentRow = _set.getRow();                     // Get current row
        int rowCount   = _set.last() ? _set.getRow() : 0;   // Determine number of rows
        
        moveCursorTo(currentRow);
        
        return rowCount;
    }

    //@Override
    public boolean getBoolean(int index) throws SQLException { return _set.getBoolean(index+1); }

   // @Override
    public float getFloat(int index) throws SQLException { return _set.getFloat(index+1); }

   // @Override
    public double getDouble(int index) throws SQLException { return _set.getDouble(index+1); }

   // @Override
    public int getInt(int index) throws SQLException { return _set.getInt(index+1); }

   // @Override
    public long getLong(int index) throws SQLException { return _set.getLong(index+1); }

   // @Override
    public String getString(int index) throws SQLException { return _set.getString(index+1); }

   // @Override
    public Date getDate(int index) throws SQLException { return _set.getDate(index+1); }

  //  @Override
    public boolean lastColumnReadWasNull() throws SQLException { return _set.wasNull(); }
    
    public int getSize() { return 0; }

    @Override
    public int getPosition() 
    {
        try {
            return _set.getRow();
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }
    

}
